# 社区controller层
from flask import Blueprint, request
import json
import service.CommunityService as CommunityService
from utils import Token

# 创建community蓝图
community = Blueprint('community', __name__)


@community.before_request
def is_login():
    return Token.verify_auth_token(request)


@community.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, HEAD, OPTIONS')
    response.headers.add('Access-Control-Allow-Headers', '*')
    return response


# 社区帖子
@community.route('/community', methods=['GET'])
def community_default():
    # 文章分页
    pagination = request.args.get('pagination', type=int, default=None)
    # 返回文章数量，默认10条
    amount = request.args.get('amount', type=int, default=10)
    # 筛选规则，1 代表按点赞排序，2 代表按时间排序，3 代表按浏览量排序
    rule = request.args.get('rule', type=int, default=None)

    result = CommunityService.service_community_default(pagination, amount, rule)
    return result


# 文章详情页
@community.route('/community/detail', methods=['GET'])
def community_detail():
    # 此处拿到id，浏览量+1
    articleId = int(request.args.get('articleId', type=str, default=None))
    uuid = int(request.args.get('uuid', type=str, default=None))

    result = CommunityService.service_community_detail(articleId, uuid)
    return result


# 发评论
@community.route('/community/sendCommit', methods=['POST'])
def community_sendCommit():
    data = request.get_data()
    data = json.loads(data)
    # 文章id
    articleId = int(data['articleId'])
    # 评论内容
    content = data['content']
    # 发评论人id
    uuid = int(data['uuid'])

    result = CommunityService.service_community_sendCommit(articleId, content, uuid)
    return result


# 文章点赞/取消点赞
@community.route('/community/articleLike', methods=['POST'])
def community_articleLike():
    data = request.get_data()
    data = json.loads(data)

    articleId = int(data['articleId'])
    # true点赞，false取消点赞
    like = data['like']
    uuid = int(data['uuid'])

    result = CommunityService.service_community_articleLike(articleId, like, uuid)
    return result


# 评论点赞/取消点赞
@community.route('/community/commitLike', methods=['POST'])
def community_commitLike():
    data = request.get_data()
    data = json.loads(data)

    commitId = int(data['commitId'])
    # true点赞，false取消点赞
    like = data['like']
    uuid = int(data['uuid'])

    result = CommunityService.service_community_commitLike(commitId, like, uuid)
    return result


# 回复点赞/取消点赞
@community.route('/community/replyLike', methods=['POST'])
def community_replyLike():
    data = request.get_data()
    data = json.loads(data)

    replyId = int(data['replyId'])
    # true点赞，false取消点赞
    like = data['like']
    uuid = int(data['uuid'])

    result = CommunityService.service_community_replyLike(replyId, like, uuid)
    return result


# 评论回复
@community.route('/community/reply', methods=['POST'])
def community_reply():
    # 后端生成一个replyId
    data = request.get_data()
    data = json.loads(data)

    commitId = int(data['commitId'])
    uuid = int(data['uuid'])
    content = data['content']

    result = CommunityService.service_community_reply(commitId, uuid, content)
    return result
